import axios from 'axios'

const service = axios.create({
  baseURL: '/gait-boot', // api base_url
  timeout: 10000 // 请求超时时间
})

const err = (error) => {
  return Promise.reject(error)
};

// 请求拦截器
service.interceptors.request.use(config => {
  const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjkzNTE5NTMsInVzZXJuYW1lIjoiYWRtaW4ifQ.K3nxc6ZtYxBhcaqugl8ihbZQlzoyEAeqhG2A4xLpvow'
  if (token) {
    config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
  return config
},(error) => {
  return Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use((response) => {
  const { data } = response
  return Promise.resolve(data)
}, err)

function getAction(url, parameter = {}) {
  return service({
    url: url,
    method: 'get',
    params: parameter
  })
}

function postAction(url, parameter = {}, query = {}) {
  return service({
    url: url,
    method: 'post',
    data: parameter,
    params: query,
  })
}

function putAction(url, parameter = {}, query = {}) {
  return service({
    url: url,
    method: 'put',
    data: parameter,
    params: query
  })
}

function deleteAction(url, parameter = {}) {
  return service({
    url: url,
    method: 'delete',
    params: parameter
  })
}

export {
  getAction,
  postAction,
  putAction,
  deleteAction
}